package com.example.allshengmedical.system.activity;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.system.presenter.ChangeHourPresenter;
import com.example.allshengmedical.system.view.IChangeHourView;
import com.example.allshengmedical.util.LogUtil;
import com.example.allshengmedical.util.SystemDateTime;

public class ChangeHourActivity extends BaseActivity implements
		IChangeHourView, OnClickListener {

	private TextView showDate, showTime;
	private Button pickDate, pickTime, cancelBtn, confirmBtn;

	private ChangeHourPresenter changeHourPresenter;

	private static final int SHOW_DATAPICK = 0;
	private static final int DATE_DIALOG_ID = 1;
	private static final int SHOW_TIMEPICK = 2;
	private static final int TIME_DIALOG_ID = 3;

	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;

	@Override
	protected int setcontent() {
		return R.layout.activity_system_changehour;
	}

	@Override
	protected void initView() {
		changeHourPresenter = new ChangeHourPresenter(this);
		changeHourPresenter.initAppHead();

		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);

		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);

		setDateTime();
		setTimeOfDay();

	}

	@Override
	protected void initData() {

	}

	@Override
	public void initAppHead() {
		TextView date, fileName;
		date = BindView(R.id.date);
		fileName = BindView(R.id.file_name);

		// 获取系统时间
		SimpleDateFormat sdfDate = new SimpleDateFormat("MM dd,yyyy");
		date.setText(sdfDate.format(new Date()));

		// 文件路径置空
		fileName.setText("");
	}

	@Override
	public void chooseDate(Button v) {

		Message msg = new Message();
		if (pickDate.equals((Button) v)) {

			msg.what = ChangeHourActivity.SHOW_DATAPICK;
		}
		ChangeHourActivity.this.dateandtimeHandler.sendMessage(msg);

	}

	@Override
	public void chooseTime(Button v) {
		Message msg = new Message();
		if (pickTime.equals((Button) v)) {
			msg.what = ChangeHourActivity.SHOW_TIMEPICK;
		}
		ChangeHourActivity.this.dateandtimeHandler.sendMessage(msg);

	}

	@Override
	public void cancel() {
		Intent intent = new Intent(ChangeHourActivity.this,
				SystemActivity.class);
		startActivity(intent);
		finish();

	}

	@Override
	public void confirm() {
		try {
			SystemDateTime.setDateTime(mYear, mMonth + 1, mDay, mHour, mMinute);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Intent intent = new Intent(ChangeHourActivity.this,
				SystemActivity.class);
		startActivity(intent);

		finish();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.showDate:

			break;
		case R.id.showTime:

			break;
		case R.id.pickDate:
			changeHourPresenter.chooseDate(pickDate);
			LogUtil.e("1111");
			break;
		case R.id.pickTime:
			changeHourPresenter.chooseTime(pickTime);
			break;
		case R.id.cancelBtn:
			changeHourPresenter.cancel();
			break;
		case R.id.confirmBtn:
			changeHourPresenter.confirm();
			break;
		default:
			break;
		}

	}

	/**
	 * 设置日期
	 */
	private void setDateTime() {
		final Calendar c = Calendar.getInstance();

		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);

		updateDateDisplay();
		/**
		 * 日期控件的事件
		 */
		DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				mYear = year;
				mMonth = monthOfYear;
				mDay = dayOfMonth;

				updateDateDisplay();
			}
		};
	}

	/**
	 * 设置时间
	 */
	private void setTimeOfDay() {
		final Calendar c = Calendar.getInstance();
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
		updateTimeDisplay();
	}

	/**
	 * 更新时间显示
	 */
	private void updateTimeDisplay() {
		showTime.setText(new StringBuilder().append(mHour).append(":")
				.append((mMinute < 10) ? "0" + mMinute : mMinute));
	}

	/**
	 * 更新日期显示
	 */
	private void updateDateDisplay() {
		showDate.setText(new StringBuilder().append(mYear).append("-")
				.append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1))
				.append("-").append((mDay < 10) ? "0" + mDay : mDay));
	}

	/**
	 * 处理日期和时间控件的Handler
	 */
	Handler dateandtimeHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ChangeHourActivity.SHOW_DATAPICK:
				showDialog(DATE_DIALOG_ID);
				break;
			case ChangeHourActivity.SHOW_TIMEPICK:
				showDialog(TIME_DIALOG_ID);
				break;
			}
		}

	};
	/**
	 * 日期控件的事件
	 */
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;

			updateDateDisplay();
		}
	};
	/**
	 * 时间控件事件
	 */
	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute;

			updateTimeDisplay();
		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
					true);
		}

		return null;
	}

}
